package com.pig.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.pig.aop.OperationLogger;
import com.pig.entity.AccountLogEntity;
import com.pig.pojo.AccountLogPojo;
import com.pig.service.AccountLogService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pig.entity.AccountUserEntity;
import com.pig.service.AccountUserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class AccountUserController {

    /**                         没有bug 神芝 超级键盘
     * ┌───┐   ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┐
     * │Esc│   │ F1│ F2│ F3│ F4│ │ F5│ F6│ F7│ F8│ │ F9│F10│F11│F12│ │P/S│S L│P/B│  ┌┐    ┌┐    ┌┐
     * └───┘   └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┘  └┘    └┘    └┘
     * ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───────┐ ┌───┬───┬───┐ ┌───┬───┬───┬───┐
     * │~ `│! 1│@ 2│# 3│$ 4│% 5│^ 6│& 7│* 8│( 9│) 0│_ -│+ =│ BacSp │ │Ins│Hom│PUp│ │N L│ / │ * │ - │
     * ├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─────┤ ├───┼───┼───┤ ├───┼───┼───┼───┤
     * │ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{ [│} ]│ | \ │ │Del│End│PDn│ │ 7 │ 8 │ 9 │   │
     * ├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤ └───┴───┴───┘ ├───┼───┼───┤ + │
     * │ Caps │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter  │               │ 4 │ 5 │ 6 │   │
     * ├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────────┤     ┌───┐     ├───┼───┼───┼───┤
     * │ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│  Shift   │     │ ↑ │     │ 1 │ 2 │ 3 │   │
     * ├─────┬──┴─┬─┴──┬┴───┴───┴───┴───┴───┴──┬┴───┼───┴┬────┬────┤ ┌───┼───┼───┐ ├───┴───┼───┤ E││
     * │ Ctrl│    │Alt │         Space         │ Alt│    │    │Ctrl│ │ ← │ ↓ │ → │ │   0   │ . │←─┘│
     * └─────┴────┴────┴───────────────────────┴────┴────┴────┴────┘ └───┴───┴───┘ └───────┴───┴───┘
     */

    /**
     * sercise
     */
    @Autowired
    private AccountUserService accountUserService;


    @Autowired
    private AccountLogService accountLogService;

    /**
     * 进入登录页面
     *
     * @return
     */
    @RequestMapping("/login")
    public String login() {
        return "login";
    }


    /**
     * 登录成功用户跳转到前台
     */
    @RequestMapping("/user/home")
    public String index() {
        return "index";
    }


    /**
     * 用户退出
     */
    @RequestMapping("/user/exit")
    @ResponseBody
    public String accountuserExitController(HttpServletRequest request, Model model, HttpSession session) {
        session = request.getSession();

        // session.removeAttribute(accountUserEntity.getAccountName());

        session.removeAttribute("accountName");

        session.removeAttribute("msg");
        System.out.println("退出成功");

        return "exit";
    }


    /**
     * 查询余额
     */
    @RequestMapping("/user/getAccountBalabce")
    @ResponseBody
    public String getAccountBalabce(HttpSession session, HttpServletRequest request) {

        System.out.println("--------------进入查询余额模式--------------");
        AccountUserEntity accountUserEntity = new AccountUserEntity();
        String accountName = (String) session.getAttribute("accountName");
        accountUserEntity = accountUserService.selectName(accountName);

        Double getAccountBalabce = accountUserEntity.getAccountBalabce();
        System.out.println("剩下余额：" + getAccountBalabce);

        String str = JSON.toJSONString(accountUserEntity);
        System.out.println(str);
        return str;
    }


    /**
     * 取款
     */
    @RequestMapping("/user/out")
    @ResponseBody
    public String out(HttpServletRequest request, Model model, HttpSession session) {
        // 运行时间
        long startTime = System.currentTimeMillis();

        System.out.println("----------------------------进入取款模式----------------------------");
        String accountOutName = (String) session.getAttribute("accountName");

        String sBalabce = request.getParameter("balabce");
        Double accountBalabce = Double.parseDouble(sBalabce);


        System.out.println(accountOutName + "---" + accountBalabce);
        AccountUserEntity accountUserEntity = new AccountUserEntity();
        accountUserEntity = accountUserService.selectName(accountOutName);

        System.out.println("accountUserEntity---" + accountUserEntity);
        AccountLogEntity accountLogEntity = new AccountLogEntity();
        System.out.println("------" + accountOutName + "------" + accountLogEntity.getAccountId());
        System.out.println("剩下余额--" + accountUserEntity.getAccountBalabce());

        if (accountUserEntity.getAccountBalabce() >= accountBalabce && accountUserEntity.getAccountBalabce() != 0) {
            boolean flag = accountUserService.out(accountOutName, accountBalabce);
            System.out.println(flag);
            if (flag) {

                System.out.println("取款成功");
                // 运行成功之后的时间
                long endTime = System.currentTimeMillis();
                float excTime = (float) (endTime - startTime) / 1000;
                System.out.println("执行时间：" + excTime + "s");
                return "200";
            }
        }
        System.out.println("余额不足，取款失败");

        // 运行成功之后的时间
        long endTime = System.currentTimeMillis();
        float excTime = (float) (endTime - startTime) / 1000;
        System.out.println("执行时间：" + excTime + "s");
        return "100";

    }


    /**
     * 存款
     */
    @RequestMapping("/user/in")
    @ResponseBody
    public String in(HttpServletRequest request, Model model, HttpSession session) {
        // 运行时间
        long startTime = System.currentTimeMillis();

        System.out.println("----------------------------进入存款模式----------------------------");

        String accountInName = (String) session.getAttribute("accountName");
        String sbalabce = request.getParameter("balabce");
        Double accountBalabce = Double.parseDouble(sbalabce);
        System.out.println(accountInName + "---" + accountBalabce);


        boolean flag = accountUserService.in(accountInName, accountBalabce);

        System.out.println(flag);
        if (flag) {
            System.out.println("存款成功");
            return "200";
        } else {
            System.out.println("存款失败");
            // 运行成功之后的时间
            long endTime = System.currentTimeMillis();
            float excTime = (float) (endTime - startTime) / 1000;
            System.out.println("执行时间：" + excTime + "s");
            return "100";
        }
    }

    /**
     * 查询日志
     */
    @RequestMapping("/user/queryAccountLog")
    @ResponseBody
    public Map<String, Object> queryAccountLog(HttpSession session, HttpServletRequest request, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println("--------------进入查询日志模式--------------");

        String accountName = (String) session.getAttribute("accountName");
        System.out.println("accountName--" + accountName);
        AccountUserEntity accountUserEntity = new AccountUserEntity();
        accountUserEntity = accountUserService.selectName(accountName);
        Integer accountId = accountUserEntity.getAccountId();
        System.out.println("accountId--" + accountId);
        System.out.println("page-- " + page);
        System.out.println("limit-- " + limit);

        PageHelper.startPage(page, limit);

        List<AccountLogPojo> pojoPageInfo = accountLogService.queryAccountLog(accountId);
        for (AccountLogPojo accountLogPojo : pojoPageInfo) {
            System.out.println(accountLogPojo);
        }


        PageInfo pageInfo = new PageInfo(pojoPageInfo);

        //保存到map集合里面
        System.out.println(pageInfo);
        map.put("data", pageInfo.getList());
        map.put("code", 0);
        map.put("count", pageInfo.getTotal());
        return map;
    }


    /**
     * 修改用户密码 updatePassword
     * <p>
     */
    @RequestMapping("/user/updatePassword")
    @ResponseBody
    public Map<String, Object> isPassword(HttpSession session, HttpServletRequest request) {
        long startTime = System.currentTimeMillis();
        System.out.println("---------------进入判断旧密码是否正确---------------");
        Map<String, Object> map = new HashMap<String, Object>();
        //获取输入的密码
        String inPass = request.getParameter("pass"); //用户名
        String accountName = (String) session.getAttribute("accountName");
        System.out.println("输入密码--" + inPass);

        //加密密码
        String md5Pass = DigestUtils.md5Hex(inPass);
        System.out.println("MD5加密后的字符串为--" + md5Pass);
        AccountUserEntity accountUserEntity = new AccountUserEntity();
        accountUserEntity = accountUserService.selectName(accountName);
        String accountPass = accountUserEntity.getAccountPassword();
        System.out.println("原密码--" + accountPass);
        if (!md5Pass.equals(accountPass)) {
            map.put("statusCode", "100");
            // 运行成功之后的时间
            long endTime = System.currentTimeMillis();
            float excTime = (float) (endTime - startTime) / 1000;
            System.out.println("执行时间：" + excTime + "s");

            return map;
        }

        System.out.println("----------------------恭喜你成功进入修改密码模式----------------------");

        /**
         * 获取新密码
         */
        String newPass = request.getParameter("newPass");

        System.out.println("newPass--" + newPass + " ** " + "accountName-- " + accountName);

        //加密密码
        String md5newPass = DigestUtils.md5Hex(newPass);
        System.out.println("MD5加密后的字符串为--" + md5newPass);

        boolean flag = accountUserService.updatePassword(accountName, md5newPass);
        System.out.println(flag);
        if (flag) {

            System.out.println("密码修改成功");
            map.put("statusCode", "200");
            System.out.println(map);
            return map;
        }
        System.out.println("密码修改失败");
        // 运行成功之后的时间
        long endTime = System.currentTimeMillis();
        float excTime = (float) (endTime - startTime) / 1000;
        System.out.println("执行时间：" + excTime + "s");
        return map;
    }


    /**
     * 判断原密码是否正确
     */
    @RequestMapping("/user/isPass")
    @ResponseBody
    public Map<String, Object> isInName(HttpSession session, HttpServletRequest request) {
        // 运行时间
        long startTime = System.currentTimeMillis();
        System.out.println("------------------------------");
        Map<String, Object> map = new HashMap<String, Object>();


        // 运行成功之后的时间
        long endTime = System.currentTimeMillis();
        float excTime = (float) (endTime - startTime) / 1000;
        System.out.println("执行时间：" + excTime + "s");
        return map;
    }


    /**
     * 转账
     */
    @RequestMapping("/user/transferAccounts")
    @ResponseBody
    public String transferAccounts(HttpSession session, HttpServletRequest request) {
        // 运行时间
        long startTime = System.currentTimeMillis();

        //获取转入用户
        String inName = request.getParameter("inName");

        String accountName = (String) session.getAttribute("accountName");

        //获取输入的金额
        String sbalabce = request.getParameter("balabce");
        Double balabce = Double.parseDouble(sbalabce);

        System.out.println(inName + " -- " + accountName + " -- " + balabce);
        AccountUserEntity accountUserEntityIs = new AccountUserEntity();
        //判断收款账户是否存在
        accountUserEntityIs = accountUserService.selectName(inName);
        System.out.println(accountUserEntityIs);
        if (null == accountUserEntityIs) {
            System.out.println("收款账户不否存在");
            return "404";
        }
        AccountUserEntity accountUserEntity = new AccountUserEntity();

        //查询余额是否充足
        accountUserEntity = accountUserService.selectName(accountName);

        if (accountUserEntity.getAccountBalabce() <= balabce) {
            System.out.println("余额不足");
            // 运行成功之后的时间
            long endTime = System.currentTimeMillis();
            float excTime = (float) (endTime - startTime) / 1000;
            System.out.println("执行时间：" + excTime + "s");
            return "100";

        }

        if (inName.equals(accountUserEntity.getAccountName())) {
            System.out.println("不能向自己转账哦");
            return "303";
        }
        System.out.println("当前用户剩下余额-- " + accountUserEntity.getAccountBalabce());
        if (accountUserEntity.getAccountBalabce() >= balabce && null != accountUserEntityIs) {
            boolean flag = accountUserService.transferAccounts(accountName, inName, balabce);
            System.out.println(flag);
            if (flag) {
                System.out.println("转账成功");
                return "200";
            }
        }

        return "500";
    }


}
